package com.admin.system.modules.system.rest;

import com.admin.common.exception.BadRequestException;
import com.admin.common.utils.FileUtil;
import com.admin.common.utils.PageResult;
import com.admin.core.entity.SysLocalStorage;
import com.admin.logging.annotation.Log;
import com.admin.system.modules.system.dto.LocalStorageQueryDTO;
import com.admin.system.modules.system.service.LocalStorageService;
import com.admin.component.web.MultipartFileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sq-lan
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "工具：本地存储管理")
@RequestMapping("/api/localStorage")
public class LocalStorageController {

    private final LocalStorageService localStorageService;

    @GetMapping
    @ApiOperation("查询文件")
    @PreAuthorize("@ss.check('storage:list')")
    public ResponseEntity<PageResult<SysLocalStorage>> queryFile(LocalStorageQueryDTO dto) {
        return new ResponseEntity<>(localStorageService.paging(dto), HttpStatus.OK);
    }

    @PostMapping
    @ApiOperation("上传文件")
    @PreAuthorize("@ss.check('storage:add')")
    public ResponseEntity<Object> add(@RequestParam String name, @RequestParam("file") MultipartFile file) {
        localStorageService.add(name, file);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @ApiOperation("上传图片")
    @PostMapping("/pictures")
    public ResponseEntity<SysLocalStorage> uploadPicture(@RequestParam MultipartFile file) {
        // 判断文件是否为图片
        String suffix = FileUtil.getExtensionName(file.getOriginalFilename());
        if (!FileUtil.IMAGE.equals(FileUtil.getFileType(suffix))) {
            throw new BadRequestException("只能上传图片");
        }
        SysLocalStorage localStorage = localStorageService.add(null, file);
        return new ResponseEntity<>(localStorage, HttpStatus.OK);
    }

    @PutMapping
    @Log(description = "修改文件")
    @ApiOperation("修改文件")
    @PreAuthorize("@ss.check('storage:edit')")
    public ResponseEntity<Object> modify(@Validated @RequestBody SysLocalStorage entity) {
        localStorageService.updateById(entity);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log(description = "删除文件")
    @DeleteMapping
    @ApiOperation("多选删除")
    public ResponseEntity<Object> remove(@RequestBody Integer[] ids) {
        localStorageService.remove(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@ss.check('storage:list')")
    public void exportFile(HttpServletResponse response, LocalStorageQueryDTO dto) throws IOException {
        List<SysLocalStorage> list = localStorageService.list(dto);

        List<Map<String, Object>> result = new ArrayList<>();
        for (SysLocalStorage localStorage : list) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("文件名", localStorage.getRealName());
            map.put("备注名", localStorage.getName());
            map.put("文件类型", localStorage.getType());
            map.put("文件大小", localStorage.getSize());
            map.put("创建者", localStorage.getCreateBy());
            map.put("创建日期", localStorage.getCreateTime());
            result.add(map);
        }
        MultipartFileUtil.downloadExcel(result, response);
    }
}
